﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using dasFX.VSTime.API;

namespace VSTimeAPI_UnitTest
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class VSTimeAPITests
    {
        public VSTimeAPITests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod()]
        public void RegisterSolutionWithSolutionPresent()
        {
            //Arrange
            var stubSolutionRepository = MockRepository.GenerateStub<ISolutionRepository>();
            var stubEventRepository = MockRepository.GenerateStub<IEventRecordRepository>();
            stubSolutionRepository.Stub(x => x.GetByFileName("Test.sln")).Return(null);

            //Act
            var eventSubmitter = new SQLSubmitEvent(string.Empty, stubSolutionRepository, stubEventRepository);
            eventSubmitter.RegisterSolution(@"C:\Test\Test.sln");

            //Assert
            stubSolutionRepository.AssertWasCalled(x => x.Save());
        }

        [TestMethod()]
        public void RegisterSolutionWithSolutionNOTPresent()
        {
            //Arrange
            var stubSolutionRepository = MockRepository.GenerateStub<ISolutionRepository>();
            var stubEventRepository = MockRepository.GenerateStub<IEventRecordRepository>();
            Solution s = new Solution(){SolutionFileName="Test.sln", SolutionFilePath=@"C:\Projects\Test.sln"};
            stubSolutionRepository.Stub(x => x.GetByFileName("Test.sln")).Return(s);

            //Act
            var eventSubmitter = new SQLSubmitEvent(string.Empty, stubSolutionRepository, stubEventRepository);
            eventSubmitter.RegisterSolution(@"C:\Test\Test.sln");

            //Assert
            stubSolutionRepository.AssertWasCalled(x => x.CreateSolution(s));
            stubSolutionRepository.AssertWasCalled(x => x.Save());
        }

        [TestMethod]
        public void RecordSolutionOpened()
        {
            //Arrange
            var stubSolutionRepository = MockRepository.GenerateStub<ISolutionRepository>();
            var stubEventRepository = MockRepository.GenerateStub<IEventRecordRepository>();
            var stubWindowPane = MockRepository.GenerateStub<EnvDTE.OutputWindowPane>();
            var stubSubmitter = MockRepository.GenerateStub<ISubmitEvent>(string.Empty, stubSolutionRepository, stubEventRepository);
           
            Solution s = new Solution() {ID = 1, SolutionFileName = "Test.sln", SolutionFilePath = @"C:\Projects\Test.sln" };
            stubSolutionRepository.Stub(x => x.GetByFileName("Test.sln")).Return(s);
            var eventSubmitter = new SQLSubmitEvent(string.Empty, stubSolutionRepository, stubEventRepository);
            EventRecorder recorder = new EventRecorder(stubSubmitter, stubWindowPane);

            //Act
            recorder.Solution_Opened(@"C:\Projects\Test.sln");

            //Assert
            Assert.AreEqual(recorder.SolutionID, 1);
            Assert.AreEqual(recorder.ProjectOpen, true);

            stubSubmitter.AssertWasCalled(x => x.RegisterEvent("SolutionOpened", "Test.sln"));
            
        
        }
       

        //RecordSolutionOpened
        //    Assert ProjectOpened is true
        //RecordSolutionClosing
        //    Assert ProjectOpened is false
        //RecordLineChanged
        //    Assert timer reset
        //RecordEnterDesignMode
        //    Assert running is false
        //RecordEnterRunMode
        //    Assert running is true
    }
}
